<template>
    <div class="high">
        <el-row>
            <el-col class="search-wrap">
                <el-row class="border-box">
                    <el-col :span="4" class="search-wrap">
                        <div class="wrap-data">启用时段</div>
                    </el-col>
                    <el-col :span="4" class="search-wrap">
                        <el-tooltip class="item" effect="dark" content="时间段格式：103000,120000  多个时间段使用 | 分割" placement="top-start">
                            <el-input
                                :disabled="showData"
                                size="mini"
                                style="width: 100%"
                                v-model="entryParamsa.paramValue"
                                @focus="changTimes"
                                @input="setHighData()"
                            ></el-input>
                        </el-tooltip>
                    </el-col>
                </el-row>
                <el-row class="border-box" v-show="showType === '4'">
                    <el-col :span="4" class="search-wrap">
                        <div class="wrap-data">业务方向</div>
                    </el-col>
                    <el-col :span="4" class="search-wrap">
                        <el-select
                            :disabled="showData"
                            size="mini"
                            multiple
                            filterable
                            collapse-tags
                            v-model="entryParamse.paramValue"
                            @focus="changTimes"
                            @change="setHighData()"
                        >
                            <el-option
                                v-for="itme in tradeDirection"
                                :key="itme.dictKey"
                                :label="itme.dictKey + ' ' + itme.dictKeyName"
                                :value="itme.dictKey"
                            ></el-option>
                        </el-select>
                    </el-col>
                </el-row>
                <el-row v-show="showType === '3'" style="margin-top: 6px">
                    <el-col :span="4" class="search-wrap">
                        <div class="wrap-data">控制模式</div>
                    </el-col>
                    <el-col :span="4">
                        <el-select
                            size="mini"
                            clearable
                            v-model="entryParams7.paramValue"
                            :disabled="showData"
                            @change="changePr7(entryParams7.paramValue)"
                        >
                            <el-option
                                v-for="itme in controlModels"
                                :key="itme.dictKey"
                                :label="itme.dictKey + ' ' + itme.dictKeyName"
                                :value="itme.dictKey"
                            >
                            </el-option>
                        </el-select>
                    </el-col>
                    <el-col :span="2" class="search-wrap">
                        <div class="wrap-data">比较天数</div>
                    </el-col>
                    <el-col :span="4">
                        <el-input-number
                            controls-position="right"
                            :disabled="showData || entryParams7.paramValue == '6'"
                            size="mini"
                            v-model="entryParams6.paramValue"
                            @change="setHighData()"
                        >
                        </el-input-number>
                    </el-col>
                    <el-col :span="2" class="search-wrap">
                        <div class="wrap-data">豁免次数</div>
                    </el-col>
                    <el-col :span="4">
                        <el-input-number
                            :disabled="showData || entryParams7.paramValue == '6'"
                            controls-position="right"
                            size="mini"
                            v-model="entryParams5.paramValue"
                            @change="setHighData()"
                        >
                        </el-input-number>
                    </el-col>
                </el-row>
                <el-row
                    class="border-box"
                    v-show="
                        showType === '0' ||
                            showType === '1' ||
                            showType === '2' ||
                            showType === '6' ||
                            showType === '8' ||
                            showType === '9' ||
                            showType === '10'
                    "
                >
                    <el-col :span="4" class="wrap-data"> 是否为绝对值 </el-col>
                    <el-col :span="4" class="wrap-chx">
                        <el-switch v-model="entryParams9.paramValue" active-value="1" inactive-value="0" :disabled="showData" @change="setHighData()">
                        </el-switch>
                    </el-col>
                </el-row>
                <el-row class="border-box" v-show="showType === '8'">
                    <el-col :span="4" class="wrap-data"> 是否买入控涨 </el-col>
                    <el-col :span="4" class="wrap-chx">
                        <el-switch v-model="entryParamsc.paramValue" active-value="1" inactive-value="0" :disabled="showData" @change="setHighData()">
                        </el-switch>
                    </el-col>
                    <el-col :span="2" class="wrap-data"> 是否卖出控跌 </el-col>
                    <el-col :span="4" class="wrap-chx">
                        <el-switch v-model="entryParamsd.paramValue" active-value="1" inactive-value="0" :disabled="showData" @change="setHighData()">
                        </el-switch>
                    </el-col>
                </el-row>
                <el-row class="border-box" v-show="showType === '3' && accountControlType == '0'">
                    <el-col :span="4" class="wrap-data"> 单向控制 </el-col>
                    <el-col :span="1" class="wrap-chx">
                        <el-switch
                            v-model="entryParams8.paramValue"
                            :disabled="entryParams7.paramValue !== '1' || showData || riskCalTypes.includes('0')"
                            active-value="1"
                            inactive-value="0"
                            @change="changParamsData(entryParams8.paramValue)"
                        >
                        </el-switch>
                    </el-col>
                </el-row>
                <el-row class="border-box" v-show="showType === '3' && entryParams8.paramValue === '1' && accountControlType == '0'">
                    <el-col :span="4" class="wrap-data"> 账户层次 </el-col>
                    <el-col :span="4" class="wrap-chx">
                        <el-select placeholder="请选择" size="mini" v-model="entryParams1.paramValue" :disabled="showData" @@change="setHighData()">
                            <el-option
                                v-for="itme in originalAccountType"
                                :key="itme.dictKey"
                                :label="itme.dictKey + ' ' + itme.dictKeyName"
                                :value="itme.dictKey"
                            ></el-option>
                        </el-select>
                    </el-col>
                    <el-col :span="2" class="wrap-data" v-show="entryParams1.paramValue === '0'"> 控制账户 </el-col>

                    <el-col :span="4" class="wrap-chx" v-show="entryParams1.paramValue === '0'">
                        <el-select
                            placeholder="请选择"
                            size="mini"
                            v-model="entryParams2.paramValue"
                            multiple
                            filterable
                            @change="setHighData()"
                            collapse-tags
                            style="width: 100%"
                        >
                            <el-option label="选择全部" value="-1"></el-option>
                            <el-option
                                v-for="item in fundInfos"
                                :key="item.value"
                                :label="item.fundCode + item.fundNameSt"
                                :value="item.fundCode"
                                :disabled="entryParams2.paramValue === '-1'"
                            >
                            </el-option>
                        </el-select>
                    </el-col>

                    <el-col :span="2" class="wrap-data" v-show="entryParams1.paramValue === '1'"> 账户组 </el-col>
                    <el-col :span="4" class="wrap-chx" v-show="entryParams1.paramValue === '1'">
                        <el-select
                            placeholder="请选择"
                            size="mini"
                            v-model="entryParams2.paramValue"
                            multiple
                            filterable
                            @change="setHighData()"
                            collapse-tags
                            style="width: 100%"
                        >
                            <el-option
                                v-for="item in fundGroup"
                                :key="item.fundGroupCode"
                                :label="item.fundGroupCode + item.fundGroupName"
                                :value="item.fundGroupCode"
                            >
                            </el-option>
                        </el-select>
                    </el-col>
                    <el-col :span="2" class="wrap-data"> 排除账户 </el-col>
                    <el-col :span="4" class="wrap-chx">
                        <el-select
                            placeholder="请选择"
                            v-model="entryParams3.paramValue"
                            multiple
                            filterable
                            size="mini"
                            collapse-tags
                            @change="setHighData()"
                        >
                            <el-option v-for="item in fundInfos" :key="item.fundCode" :label="item.fundCode + item.fundNameSt" :value="item.fundCode">
                            </el-option>
                        </el-select>
                    </el-col>
                    <el-col :span="2" class="wrap-data"> 排除账户组 </el-col>
                    <el-col :span="4" class="wrap-chx">
                        <el-select
                            placeholder="请选择"
                            multiple
                            filterable
                            size="mini"
                            collapse-tags
                            v-model="entryParams4.paramValue"
                            @change="setHighData()"
                        >
                            <el-option v-for="itme in fundGroup" :key="itme.value" :label="itme.label" :value="itme.value"></el-option>
                        </el-select>
                    </el-col>
                </el-row>
                <el-row class="border-box" v-show="showType !== '3' && showType !== '5'">
                    <el-col :span="4" class="wrap-data"> 当分母为0时，返回结果为合规 </el-col>
                    <el-col :span="2" class="wrap-chx">
                        <el-switch v-model="entryParamsf.paramValue" active-value="1" inactive-value="0" :disabled="showData" @change="setHighData()">
                        </el-switch>
                    </el-col>
                </el-row>

                <el-row class="border-box" v-show="showType !== '3' && showType !== '5'">
                    <el-col :span="4" class="wrap-data"> 是否读取虚拟持仓 </el-col>
                    <el-col :span="2" class="wrap-chx">
                        <el-switch v-model="entryParamsh.paramValue" active-value="1" inactive-value="0" :disabled="showData" @change="setHighData()">
                        </el-switch>
                    </el-col>
                </el-row>

                <el-row class="border-box" v-show="showType !== '3' && showType !== '5'">
                    <el-col :span="4" class="wrap-data"> 标的券指令是否要与CRMW指令批量审批 </el-col>
                    <el-col :span="2" class="wrap-chx">
                        <el-switch v-model="entryParamsk.paramValue" active-value="1" inactive-value="0" :disabled="showData" @change="setHighData()">
                        </el-switch>
                    </el-col>
                </el-row>
            </el-col>
        </el-row>
    </div>
</template>
<script lang="ts">
import { Component, Prop, Vue, Watch } from 'vue-property-decorator';
import { mixins } from 'vue-class-component';
import DictionConstUtil, { SGU_CACHE, FNFundGroup } from '@/utils/diction-const-util';
import RiskEmitter from '@/common/mixis/risk-emitter.vue';
import { isArray } from 'xe-utils/methods';

@Component({
    name: 'HighSetting',
    components: {},
})
export default class HighSetting extends mixins(RiskEmitter) {
    @Prop() entryParamsList!: any[];
    @Prop() fxType!: any;
    @Prop() showData: boolean;
    @Prop() accountControlType: string;
    @Prop() riskCalTypes: string[];
    showType: string = '0';
    controlModels: any = [
        { dictKey: '1', dictKeyName: '不能有买有卖' },
        { dictKey: '2', dictKeyName: '不允许买价>=卖价' },
        { dictKey: '6', dictKeyName: '未成部分不能买价大于卖价,卖价不能小于买价' },
    ];
    entryParams: any[] = [{ paramType: null, paramValue: null, compareType: null }];
    tradeDirection: any = [];
    originalAccountType: any[] = [];
    fundInfos: any[] = [];
    fundGroup: any[] = [];
    entryParamsa: any = {
        paramType: 'a',
        paramValue: '',
        compareType: 0,
    };
    entryParamsc: any = {
        paramType: 'c',
        paramValue: '',
        compareType: 0,
    };
    entryParamsd: any = {
        paramType: 'd',
        paramValue: '',
        compareType: 0,
    };
    entryParamse: any = {
        paramType: 'e',
        paramValue: '',
        compareType: 0,
    };
    entryParams1: any = {
        paramType: '1',
        paramValue: '',
        compareType: 0,
    };
    entryParams2: any = {
        paramType: '2',
        paramValue: '',
        compareType: 0,
    };
    entryParams3: any = {
        paramType: '3',
        paramValue: '',
        compareType: 0,
    };
    entryParams4: any = {
        paramType: '4',
        paramValue: '',
        compareType: 0,
    };
    entryParams5: any = {
        paramType: '5',
        paramValue: 0,
        compareType: 0,
    };
    entryParams6: any = {
        paramType: '6',
        paramValue: 0,
        compareType: 0,
    };
    entryParams7: any = {
        paramType: '7',
        paramValue: '',
        compareType: 0,
    };
    entryParams8: any = {
        paramType: '8',
        paramValue: '0',
        compareType: 0,
    };
    entryParams9: any = {
        paramType: '9',
        paramValue: '0',
        compareType: 0,
    };
    entryParamsf: any = {
        paramType: 'f',
        paramValue: '0',
        compareType: 0,
    };

    // 是否读取虚拟持仓
    entryParamsh: any = {
        paramType: 'h',
        paramValue: '0',
        compareType: 0,
    };

    // 标的券指令是否要与CRMW指令批量审批
    entryParamsk: any = {
        paramType: 'K',
        paramValue: '0',
        compareType: 0,
    };

    created() {
        this.$on('addTables', this.changEntryType);
    }

    async mounted() {
        console.time('高级设置:');

        // this.controlModels = DictionConstUtil.PojoToList(DictionConstUtil.ControlModel);
        this.tradeDirection = await SGU_CACHE('TradeDirection', '/dict/tradeDirections', []);
        this.originalAccountType = DictionConstUtil.PojoToList(DictionConstUtil.OriginalAccountType);
        this.fundInfos = await SGU_CACHE('FundInfo', '/fundInfo/list', []);
        this.fundGroup = await FNFundGroup();
        this.changTimes();
        console.timeEnd('高级设置:');
    }

    beforeDestroy() {
        this.$off();
        console.log('高级设置销毁');
    }

    changEntryType(val: any) {
        if (this.showType !== val) {
            this.resetSingleControl();
        }
        this.showType = val;
    }

    // 深度监听传过来的数据 如果有数据就渲染上去
    @Watch('entryParamsList', { immediate: true, deep: true })
    watchEntryentryParamsList() {
        // if (!this.entryParamsList.length) {
        //     return;
        // }

        let ParamsList = JSON.parse(JSON.stringify(this.entryParamsList));
        this.$nextTick(() => {
            for (let i = 0; i < ParamsList.length; i++) {
                if (ParamsList[i].paramType === '1') {
                    this.entryParams1.paramValue = ParamsList[i].paramValue;
                } else if (ParamsList[i].paramType === '2') {
                    this.entryParams2.paramValue = ParamsList[i].paramValue.split(',');
                } else if (ParamsList[i].paramType === '3') {
                    this.entryParams3.paramValue = ParamsList[i].paramValue.split(',');
                } else if (ParamsList[i].paramType === '4') {
                    this.entryParams4.paramValue = ParamsList[i].paramValue.split(',');
                } else if (ParamsList[i].paramType === '5') {
                    this.entryParams5.paramValue = ParamsList[i].paramValue;
                } else if (ParamsList[i].paramType === '6') {
                    this.entryParams6.paramValue = ParamsList[i].paramValue;
                } else if (ParamsList[i].paramType === '7') {
                    this.entryParams7.paramValue = ParamsList[i].paramValue;
                } else if (ParamsList[i].paramType === '8') {
                    this.entryParams8.paramValue = ParamsList[i].paramValue;
                } else if (ParamsList[i].paramType === '9') {
                    this.entryParams9.paramValue = ParamsList[i].paramValue;
                } else if (ParamsList[i].paramType === 'a') {
                    this.entryParamsa.paramValue = ParamsList[i].paramValue;
                } else if (ParamsList[i].paramType === 'c') {
                    this.entryParamsc.paramValue = ParamsList[i].paramValue;
                } else if (ParamsList[i].paramType === 'd') {
                    this.entryParamsd.paramValue = ParamsList[i].paramValue;
                } else if (ParamsList[i].paramType === 'e') {
                    this.entryParamse.paramValue = ParamsList[i].paramValue.split(',');
                } else if (ParamsList[i].paramType === 'f') {
                    this.entryParamsf.paramValue = ParamsList[i].paramValue;
                } else if (ParamsList[i].paramType === 'h') {
                    this.entryParamsh.paramValue = ParamsList[i].paramValue;
                } else if (ParamsList[i].paramType === 'K') {
                    this.entryParamsk.paramValue = ParamsList[i].paramValue;
                }
            }
            this.setHighData();
        });
    }

    @Watch('fxType', { immediate: true, deep: true })
    watchFxType(newValue, oldValue) {
        this.showType = this.fxType;
        // this.setHighData();
        if (oldValue !== undefined) {
            this.setHighData();
        }
    }

    @Watch('riskCalTypes')
    watchRiskCalTypes(newData) {
        if (newData.includes('0')) {
            if (this.entryParams7.paramValue === '1') {
                console.log('【同向反向支持静态】配置条目支持静态时，选中对应的高级设置中，单向控制必须置灰');
                this.entryParams8.paramValue = '0';
                this.changParamsData('0');
            }
        }
    }

    @Watch('entryParamse', { immediate: true, deep: true })
    watchentryParamse() {
        this.changTimes();
    }

    @Watch('accountControlType', { immediate: true, deep: true })
    watchAccountControlType() {
        if (this.accountControlType !== '0') {
            this.entryParams8.paramValue = '0';
            this.changParamsData('0');
        }
    }

    changTimes(): void {
        if (!isArray(this.entryParamse.paramValue) && this.entryParamse.paramValue) {
            this.entryParamse.paramValue = this.entryParamse.paramValue.split(',');
        }
    }

    changePr7(val: any) {
        if (val == '1') {
            if (this.accountControlType !== '0' || this.riskCalTypes.includes('0')) {
                this.entryParams8.paramValue = '0';
                this.changParamsData('0');
            } else {
                this.entryParams8.paramValue = val;
                this.changParamsData('1');
            }
        } else if (val == '6') {
            // 为6 比较天数和豁免次数置为0
            this.entryParams5.paramValue = '0';
            this.entryParams6.paramValue = '0';
            this.entryParams8.paramValue = '0';
            this.changParamsData('0');
        } else {
            this.entryParams8.paramValue = '0';
            this.changParamsData('0');
        }
    }

    setHighData(): void {
        this.entryParams = [];
        this.entryParams.push(this.entryParamsa);
        if (this.showType === '0' || this.showType === '1' || this.showType === '2' || this.showType === '6' || this.showType === '9') {
            this.entryParams.push(this.entryParams9, this.entryParamsf, this.entryParamsh, this.entryParamsk);
        } else if (this.showType === '4') {
            this.entryParams.push(this.entryParamse, this.entryParamsf, this.entryParamsh, this.entryParamsk);
        } else if (this.showType === '8') {
            this.entryParams.push(this.entryParams9, this.entryParamsc, this.entryParamsd, this.entryParamsf, this.entryParamsh, this.entryParamsk);
        } else if (this.showType === '3') {
            this.entryParams.push(
                this.entryParams7,
                this.entryParams6,
                this.entryParams5,
                this.entryParams8,
                this.entryParams1,
                this.entryParams2,
                this.entryParams3,
                this.entryParams4
            );
        }
        // 剔除空值的对象
        let newEntryParams = [];
        for (let i = 0; i < this.entryParams.length; i++) {
            if (this.entryParams[i].paramValue !== '' || this.entryParams[i].paramValue.length > 0) {
                newEntryParams.push(this.entryParams[i]);
            }
        }
        if (newEntryParams) {
            for (let j = newEntryParams.length - 1; j >= 0; j--) {
                if (newEntryParams[j].paramValue.length === 0) {
                    newEntryParams.splice(j, 1);
                }
            }
        }
        this.$emit('set-high-data', newEntryParams);
    }

    /**
     * 重置所有数据
     */
    resetSingleControl() {
        this.entryParamsa.paramValue = '';
        this.entryParamsc.paramValue = '';
        this.entryParamsd.paramValue = '';
        this.entryParamse.paramValue = '';
        this.entryParams1.paramValue = '';
        this.entryParams2.paramValue = '';
        this.entryParams3.paramValue = '';
        this.entryParams4.paramValue = '';
        this.entryParams5.paramValue = 0;
        this.entryParams6.paramValue = 0;
        this.entryParams7.paramValue = '';
        this.entryParams8.paramValue = '0';
        this.entryParams9.paramValue = '0';
        this.entryParamsf.paramValue = '0';
        this.entryParamsh.paramValue = '0';
        this.entryParamsk.paramValue = '0';
    }

    // 监听单向控制
    changParamsData(val: any) {
        if (val == 1) {
            this.entryParams6.paramValue = 1;
            this.entryParams5.paramValue = 0;
        }
        this.setHighData();
    }
}
</script>
<style lang="scss" scoped>
.high {
    color: #222;
    padding: 10px;

    .search-wrap {
        padding: 2px 10px 1px 1px;
        margin-right: 5px;

        .border-box {
            margin-top: 6px;
        }

        .wrap-data {
            text-align: right;
            padding-right: 3px;
            margin-bottom: 5px;
        }

        .wrap-datas {
            padding-right: 3px;
            margin-left: 10px;
            margin-bottom: 5px;
        }

        .wrap-chx {
            // text-align: right;
            padding-left: 5px;
        }
    }
}
/deep/ .el-select__tags {
    flex-wrap: nowrap;
    overflow: hidden;
}
</style>
